<template>
  <div class="login">
    <div class="box">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        status-icon
      >
        <el-form-item>
          <div style="text-align: center">
            <img src="@/assets/common/login-logo.png" alt="">
          </div>
        </el-form-item>

        <el-form-item prop="mobile">
          <el-input
            v-model="loginForm.mobile"
            type="text"
            prefix-icon="el-icon-user-solid"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-lock"
            @keyup.enter.native="loginBtn"
          />
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%" @click="loginBtn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        mobile: '13800000002', // string	必须		手机号
        password: '123456' // string	必须		密码
      },
      loginRules: {
        mobile: [
          { required: true, message: '手机号是必填项', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '密码是必填项', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '请输入6到12位的密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    loginBtn() {
      this.$refs.loginForm.validate(async(valid) => {
        //   if (valid) {
        //     const res = await sysLogin(this.loginForm)
        //     this.$message.success('验证成功')
        //     console.log(res)
        //   }
        if (valid) {
          // 此处不加await 会先执行2
          await this.$store.dispatch('user/toLogin', this.loginForm)
          // console.log(1)
          // 跳转到主页
          // this.$router.push('/')
          // 跳转到之前退出来的页面
          const _redirect = this.$route.query.redirect || '/'
          this.$router.push(_redirect)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background: url(~@/assets/common/login.jpg) no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    padding: 35px 35px 15px;
    .el-button {
      background-color: #407ffe;
      border: 0;
      color: #fff;
      font-size: 16px;
    }
  }
}
</style>
